package com.zhora.service.ai.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhora.common.domain.page.PageDataGridRespDTO;
import com.zhora.common.errcode.CommonCode;
import com.zhora.common.utils.NumberUtil;
import com.zhora.common.utils.ValidateUtil;
import com.zhora.db.common.util.PageDataGridRespUtil;
import com.zhora.db.common.util.PageQueryUtil;
import com.zhora.dto.ai.AiLibraryDocSegmentDTO;
import com.zhora.dto.ai.search.AiLibraryDocSegmentSearchDTO;
import com.zhora.entity.ai.AiLibraryDocEntity;
import com.zhora.entity.ai.AiLibraryDocSegmentEntity;
import com.zhora.mapper.ai.AiLibraryDocSegmentMapper;
import com.zhora.service.ai.IAiLibraryDocSegmentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ai文档-片段(ai_library_doc_segment)表服务实现类
 *
 * @author zhehen.lu
 * @since 2025-09-25 11:19:16
 */
@Service
public class AiLibraryDocSegmentServiceImpl extends ServiceImpl<AiLibraryDocSegmentMapper, AiLibraryDocSegmentEntity> implements IAiLibraryDocSegmentService {

    /**
     * 根据条件分页查询ai文档-片段列表
     *
     * @param searchDTO
     * @return {@link PageDataGridRespDTO< AiLibraryDocSegmentDTO>}
     * @date 2025-09-25 11:19:16
     * @author zhehen.lu
     */
    @Override
    public PageDataGridRespDTO<AiLibraryDocSegmentDTO> listPage(AiLibraryDocSegmentSearchDTO searchDTO) {
        ValidateUtil.notNull(searchDTO, CommonCode.PARMA_NOT_NULL);

        LambdaQueryChainWrapper<AiLibraryDocSegmentEntity> wrapper = getWrapper(searchDTO);

        PageQueryUtil<AiLibraryDocSegmentEntity, AiLibraryDocSegmentSearchDTO> pageQueryUtil = new PageQueryUtil<>(
                AiLibraryDocSegmentEntity.class,
                PageQueryUtil.TypeEnum.PARAM_PRIORITY_DEFAULT_PRIMARY,
                searchDTO
        );

        IPage<AiLibraryDocSegmentEntity> page = wrapper.page(pageQueryUtil.buildPageObj());

        return PageDataGridRespUtil.convert(page, AiLibraryDocSegmentDTO.class);
    }

    /**
     * 创建ai文档-片段
     *
     * @param dto
     * @date 2025-09-25 11:19:16
     * @author zhehen.lu
     */
    @Override
    public void create(AiLibraryDocSegmentDTO dto) {
        ValidateUtil.notNull(dto, CommonCode.PARMA_NOT_NULL);
        AiLibraryDocSegmentEntity entity = BeanUtil.copyProperties(dto, AiLibraryDocSegmentEntity.class);

        save(entity);
    }

    /**
     * 依据ID获取ai文档-片段详情
     *
     * @param id
     * @return {@link AiLibraryDocSegmentDTO}
     * @date 2025-09-25 11:19:16
     * @author zhehen.lu
     */
    @Override
    public AiLibraryDocSegmentDTO getDetailById(Long id) {
        AiLibraryDocSegmentSearchDTO searchDTO = new AiLibraryDocSegmentSearchDTO();
        searchDTO.setId(id);
        searchDTO.setDelFlag(Boolean.FALSE);

        return getDetail(searchDTO);
    }

    /**
     * 获取ai文档-片段详情
     *
     * @param searchDTO
     * @return {@link AiLibraryDocSegmentDTO}
     * @date 2025-09-25 11:19:16
     * @author zhehen.lu
     */
    @Override
    public AiLibraryDocSegmentDTO getDetail(AiLibraryDocSegmentSearchDTO searchDTO) {
        LambdaQueryChainWrapper<AiLibraryDocSegmentEntity> wrapper = getWrapper(searchDTO);
        AiLibraryDocSegmentEntity entity = wrapper
                .last("LIMIT 1")
                .one();

        return BeanUtil.copyProperties(entity, AiLibraryDocSegmentDTO.class);
    }

    /**
     * 依据ID更新ai文档-片段数据
     *
     * @param dto
     * @date 2025-09-25 11:19:16
     * @author zhehen.lu
     */
    @Override
    public void updateById(AiLibraryDocSegmentDTO dto) {
        ValidateUtil.notNull(dto, CommonCode.PARMA_NOT_NULL);
        AiLibraryDocSegmentEntity entity = BeanUtil.copyProperties(dto, AiLibraryDocSegmentEntity.class);

        updateById(entity);
    }

    /**
     * 获取ai文档-片段列表
     *
     * @param searchDTO
     * @return {@link List< AiLibraryDocSegmentDTO>}
     * @date 2025-09-25 11:19:16
     * @author zhehen.lu
     */
    @Override
    public List<AiLibraryDocSegmentDTO> list(AiLibraryDocSegmentSearchDTO searchDTO) {
        ValidateUtil.notNull(searchDTO, CommonCode.PARMA_NOT_NULL);

        LambdaQueryChainWrapper<AiLibraryDocSegmentEntity> wrapper = getWrapper(searchDTO);

        List<AiLibraryDocSegmentEntity> entityList = wrapper.list();

        return BeanUtil.copyToList(entityList, AiLibraryDocSegmentDTO.class);
    }

    /**
     * 获取ai文档-片段的Wrapper对象
     *
     * @param searchDTO
     * @return {@link LambdaQueryChainWrapper< AiLibraryDocSegmentEntity>}
     * @date 2025-09-25 11:19:16
     * @author zhehen.lu
     */
    private LambdaQueryChainWrapper<AiLibraryDocSegmentEntity> getWrapper(AiLibraryDocSegmentSearchDTO searchDTO) {
        return lambdaQuery()
                .eq(NumberUtil.isGtZero(searchDTO.getId()), AiLibraryDocSegmentEntity::getId, searchDTO.getId())
                .eq(NumberUtil.isGtZero(searchDTO.getDocId()), AiLibraryDocSegmentEntity::getDocId, searchDTO.getDocId())
                .eq(StringUtils.isNoneEmpty(searchDTO.getEmbeddingId()), AiLibraryDocSegmentEntity::getEmbeddingId, searchDTO.getEmbeddingId())
                .eq(null != searchDTO.getDelFlag(), AiLibraryDocSegmentEntity::getDelFlag, searchDTO.getDelFlag());
    }

}
